using VOL.demo.IRepositories;
using VOL.demo.IServices;
using VOL.Core.BaseProvider;
using VOL.Entity.DomainModels;
using VOL.Core.Utilities;
using System.Linq;
using VOL.Core.Extensions;

namespace VOL.demo.Services
{
    public partial class GoodsBathService
    {
        // Repository已在基类中注入，这里可以直接使用repository属性
        /// <summary>
        /// 添加货品批次信息
        /// </summary>
        /// <param name="goodsBatch">货品批次信息</param>
        /// <returns></returns>
        public WebResponseContent AddGoodsBatch(GoodsBatchModel goodsBatch)
        {
            return repository.DbContextBeginTransaction(() =>
            {
                try
                {
                    // 验证必填字段
                    if (goodsBatch.GoodsId <= 0)
                        return new WebResponseContent().Error("货品ID不能为空");
                    if (goodsBatch.WarehouseId <= 0)
                        return new WebResponseContent().Error("仓库ID不能为空");
                    if (goodsBatch.Quantity <= 0)
                        return new WebResponseContent().Error("数量必须大于0");

                    // 检查批次号是否已存在
                    if (!string.IsNullOrEmpty(goodsBatch.BatchNo))
                    {
                        var existingBatch = repository.Find(x => x.BatchNo == goodsBatch.BatchNo 
                            && x.GoodsId == goodsBatch.GoodsId 
                            && x.WarehouseId == goodsBatch.WarehouseId 
                            && x.IsDel == 0).FirstOrDefault();
                        if (existingBatch != null)
                        {
                            return new WebResponseContent().Error("该批次号在该仓库中已存在");
                        }
                    }

                    goodsBatch.SetCreateDefaultVal();
                    goodsBatch.IsDel = 0;
                    repository.Add(goodsBatch, true);
                    
                    return new WebResponseContent().OK("货品批次添加成功", goodsBatch);
                }
                catch (Exception ex)
                {
                    return new WebResponseContent().Error($"货品批次添加失败：{ex.Message}");
                }
            });
        }

        /// <summary>
        /// 批量添加货品批次信息
        /// </summary>
        /// <param name="goodsBatches">货品批次列表</param>
        /// <returns></returns>
        public WebResponseContent BatchAddGoodsBatch(List<GoodsBatchModel> goodsBatches)
        {
            return repository.DbContextBeginTransaction(() =>
            {
                try
                {
                    foreach (var batch in goodsBatches)
                    {
                        // 验证必填字段
                        if (batch.GoodsId <= 0 || batch.WarehouseId <= 0 || batch.Quantity <= 0)
                        {
                            return new WebResponseContent().Error("存在无效的批次数据");
                        }

                        batch.SetCreateDefaultVal();
                        batch.IsDel = 0;
                    }

                    repository.AddRange(goodsBatches, true);
                    return new WebResponseContent().OK("批次信息批量添加成功", goodsBatches);
                }
                catch (Exception ex)
                {
                    return new WebResponseContent().Error($"批次信息批量添加失败：{ex.Message}");
                }
            });
        }

        /// <summary>
        /// 根据货品ID和仓库ID获取批次信息
        /// </summary>
        /// <param name="goodsId">货品ID</param>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        public List<GoodsBatchModel> GetBatchesByGoodsAndWarehouse(long goodsId, long warehouseId)
        {
            return repository.Find(x => x.GoodsId == goodsId 
                && x.WarehouseId == warehouseId 
                && x.IsDel == 0)
                .OrderByDescending(x => x.CreateDate)
                .ToList();
        }

        /// <summary>
        /// 根据批次号获取批次信息
        /// </summary>
        /// <param name="batchNo">批次号</param>
        /// <returns></returns>
        public GoodsBatchModel GetBatchByBatchNo(string batchNo)
        {
            if (string.IsNullOrEmpty(batchNo))
                return null;

            return repository.Find(x => x.BatchNo == batchNo && x.IsDel == 0).FirstOrDefault();
        }

        /// <summary>
        /// 更新货品批次信息
        /// </summary>
        /// <param name="goodsBatch">货品批次信息</param>
        /// <returns></returns>
        public WebResponseContent UpdateGoodsBatch(GoodsBatchModel goodsBatch)
        {
            return repository.DbContextBeginTransaction(() =>
            {
                try
                {
                    var existingBatch = repository.Find(x => x.Id == goodsBatch.Id && x.IsDel == 0).FirstOrDefault();
                    if (existingBatch == null)
                    {
                        return new WebResponseContent().Error("批次信息不存在");
                    }

                    // 验证必填字段
                    if (goodsBatch.GoodsId <= 0)
                        return new WebResponseContent().Error("货品ID不能为空");
                    if (goodsBatch.WarehouseId <= 0)
                        return new WebResponseContent().Error("仓库ID不能为空");
                    if (goodsBatch.Quantity <= 0)
                        return new WebResponseContent().Error("数量必须大于0");

                    // 更新字段
                    existingBatch.GoodsId = goodsBatch.GoodsId;
                    existingBatch.WarehouseId = goodsBatch.WarehouseId;
                    existingBatch.BatchNo = goodsBatch.BatchNo;
                    existingBatch.ProductionDate = goodsBatch.ProductionDate;
                    existingBatch.Quantity = goodsBatch.Quantity;
                    existingBatch.ExpireDate = goodsBatch.ExpireDate;
                    existingBatch.Remark = goodsBatch.Remark;
                    existingBatch.SetModifyDefaultVal();

                    repository.Update(existingBatch, true);
                    return new WebResponseContent().OK("货品批次更新成功");
                }
                catch (Exception ex)
                {
                    return new WebResponseContent().Error($"货品批次更新失败：{ex.Message}");
                }
            });
        }

        /// <summary>
        /// 删除货品批次信息
        /// </summary>
        /// <param name="id">批次ID</param>
        /// <returns></returns>
        public WebResponseContent DeleteGoodsBatch(long id)
        {
            return repository.DbContextBeginTransaction(() =>
            {
                try
                {
                    var existingBatch = repository.Find(x => x.Id == id && x.IsDel == 0).FirstOrDefault();
                    if (existingBatch == null)
                    {
                        return new WebResponseContent().Error("批次信息不存在");
                    }

                    existingBatch.IsDel = 1;
                    existingBatch.SetModifyDefaultVal();
                    repository.Update(existingBatch, true);

                    return new WebResponseContent().OK("货品批次删除成功");
                }
                catch (Exception ex)
                {
                    return new WebResponseContent().Error($"货品批次删除失败：{ex.Message}");
                }
            });
        }

        /// <summary>
        /// 获取即将过期的批次信息
        /// </summary>
        /// <param name="days">提前天数</param>
        /// <returns></returns>
        public List<GoodsBatchModel> GetExpiringSoonBatches(int days)
        {
            var expireDate = DateTime.Now.AddDays(days);
            return repository.Find(x => x.ExpireDate <= expireDate 
                && x.ExpireDate >= DateTime.Now
                && x.IsDel == 0)
                .OrderBy(x => x.ExpireDate)
                .ToList();
        }

        /// <summary>
        /// 根据仓库ID获取所有批次信息
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        public List<GoodsBatchModel> GetBatchesByWarehouse(long warehouseId)
        {
            return repository.Find(x => x.WarehouseId == warehouseId && x.IsDel == 0)
                .OrderByDescending(x => x.CreateDate)
                .ToList();
        }
    }
}
